<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Customer</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>物品日志</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">渠道类型</label>
                            <div class="layui-input-inline">
                                <div id="serverGroup"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">选择区服:</label>
                            <div class="layui-input-inline">
                                <select name="gameArea" class="layui-select" >
                                    <option v-for="(item,index) in services" :value="item.server_id" :selected="index==0">{{item.name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="memberName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i>搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="showItem">查看摆摊物品</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="showTempBylist">查看临时仓库</a>
        </script>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="export" auth="equipment:export">导出</button>
    </div>
</script>
<script id="itemList" type="text/html">
    <table class="layui-table">
        <colgroup>
            <col width="150">
            <col width="150">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th>角色ID</th>
            <th>物品名称</th>
            <th>物品数量</th>
            <th>唯一ID</th>
        </tr>
        </thead>
        <tbody>
        {{# layui.each(d.itemList,function(index,row){ }}
            <tr>
                <td>{{d.role_id}}</td>
                <td>{{getEquipName(row.base.id)}}</td>
                <td>{{row.base.count}}</td>
                <td>{{row.base.uniqueid}}</td>
            </tr>
        {{# }) }}
        </tbody>
    </table>
</script>

<script id="itemList2" type="text/html">
    <table class="layui-table">
        <colgroup>
            <col width="150">
            <col width="150">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th>角色ID</th>
            <th>物品名称</th>
            <th>物品数量</th>
            <th>唯一ID</th>
        </tr>
        </thead>
        <tbody>
        {{# layui.each(d.itemList,function(index,row){ }}
        <tr>
            <td>{{d.role_id}}</td>
            <td>{{getEquipName(row.base.id)}}</td>
            <td>{{row.base.count}}</td>
            <td>{{row.base.uniqueid}}</td>
        </tr>
        {{# }) }}
        </tbody>
    </table>
</script>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script src="/xm-select-v1.2.4/xm-select.js"></script>
<script>
    var maps = getEnum('map')
    var equipments = getEnum('equip')
    var serverGroup = getEnum('serverGroup')

    function getMapName(mapId) {
        for(var k in maps){
            if (maps[k].id === mapId){
                return maps[k].name;
            }
        }
        return mapId;
    }
    function getEquipName(itemId) {
        for(var k in equipments){
            if (equipments[k].id === itemId){
                return equipments[k].name;
            }
        }
        return itemId;
    }

    new Vue({
        el: "#app",
        data: {
            services: JSON.parse(localStorage.getItem('services')),
        },
        mounted: function () {
            var _this = this;

            layui.use(['form', 'table','laydate'], function () {
                 $ = layui.jquery,
                    form = layui.form,
                    table = layui.table,
                     laytpl=layui.laytpl;



                var dataSg = [];
                for(var k in serverGroup){
                    var v = serverGroup[k];
                    dataSg.push({
                        name:v.name,
                        value:v.id,
                    })
                }

                xmSelect.render({
                    el: '#serverGroup',
                    name:"serverGroup",
                    language: 'zn',
                    data: dataSg,
                    on: function(data){
                        // 选择器值发生变化时触发
                        console.log('DATA:', data);
                        var selectedValues = data.arr.map(item => item.value);

                        // 在这里编写您想要执行的方法
                        handleServerGroupChange(selectedValues);
                    }
                })

                function handleServerGroupChange(selectedValues) {
                    // 在这里编写您想要执行的逻辑
                    if (selectedValues.length === 0) {
                        // 如果 selectedValues 为空数组,则不发送请求
                        console.log('No server groups selected.');
                        return;
                    }
                    console.log('Selected server groups:', selectedValues);
                    var url = '/admin/server/serverList';
                    var params = { groups: selectedValues };
                    var paramsString = $.param(params);
                    $.get(url + '?' + paramsString, function(res) {
                        console.log('res:', res.data);
                        _this.$set(_this, 'services', res.data);
                        _this.$nextTick(function() {
                            console.log('进来了么:');
                            form.render();
                        });
                    });
                }
                table.render({
                    elem: '#currentTableId',
                    where:form.val('table-search'),
                    url: '/admin/stall/paginate',
                    defaultToolbar: ['filter', 'print' ],
                    toolbar: '#toolbarDemo',

                    cols: [[
                        {title: 'ID', field:'id' },
                        {title: '账号名', field:'memberName', },
                        {title: '角色ID', field:'role_id' },
                        {title: '昵称', field:'role_nick' },
                        {title: '摊位名称', field:'name' },
                        {title: '资金', field:'money' },
                        {title: '地图', field:'target_map',templet:function(row){
                            return getMapName(row.target_map);
                            } },
                        {title: '位置', field:'pos' },
                        {title: '剩余时间', field:'valid_time' },
                        {title: '操作',toolbar:'#currentTableBar',minWidth:230,align: "center"},
                    ]],
                    limits: [10, 15, 20, 25, 50, 100],
                    limit: 15,
                    page: true,
                    skin: 'line'
                });
                // 监听搜索操作
                form.on('submit(data-search-btn)', function () {
                    //执行搜索重载
                    table.reload('currentTableId', {
                        page: {
                            curr: 1
                        }
                        , where:form.val('table-search'),
                    }, 'data');
                    return false;
                });
                table.on('toolbar(currentTableFilter)', function (obj) {
                    if(obj.event === 'export'){ //导出
                        var searchData = form.val('table-search');
                        searchData.page = 1;
                        searchData.limit = 9999999;
                        var layerIndex = layer.msg("请求中请耐心等待...",{icon:16,time:999999999});
                        $.get('/admin/equipment/export',searchData,function(res){
                            layer.close(layerIndex)
                            browserDownload("/" + res.data);
                        });
                    }
                });
                table.on('tool(currentTableFilter)', function (obj) {
                    if (obj.event==='showItem') {
                        layer.open({
                            type: 1,
                            title: '查看摆摊物品',
                            area: ['780px', '500px'],
                            shade: 0.6,
                            shadeClose: true,
                            content: laytpl($('#itemList').prop("innerHTML")).render({
                                role_id:obj.data.role_id,
                                itemList: obj.data.sell_item_list.data,
                            })
                        });
                    } else if(obj.event ==='showTempBylist'){

                        layer.open({
                            type: 1,
                            title: '查看摆摊物品',
                            area: ['780px', '500px'],
                            shade: 0.6,
                            shadeClose: true,
                            content: laytpl($('#itemList2').prop("innerHTML")).render({
                                role_id:obj.data.role_id,
                                itemList: JSON.parse( obj.data.temp_buy_list ),
                            })
                        });
                    }
                });

            });
        }
    })
</script>
</body>
</html>